//
//  HWDetailPictureController.m
//  JWC
//
//  Created by lanou on 16/4/6.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWDetailPictureController.h"
#import "UIImageView+WebCache.h"
#import "SVProgressHUD.h"
#import "DALabeledCircularProgressView.h"


@interface HWDetailPictureController ()

// 保存
@property (nonatomic, strong)UIButton *saveBtn;
// 分享
@property (nonatomic, strong)UIButton *shareBtn;
// 评论
@property (nonatomic, strong)UIButton *commentBtn;
// 返回
@property (nonatomic, strong)UIButton *backBtn;
// 背景view
@property (nonatomic, strong)UIView *backgroundView;
@property (nonatomic, strong)UIImageView *imageView;
@property (nonatomic, strong)DALabeledCircularProgressView *progressView;

@property (nonatomic,strong) UIScrollView *scrollView;

@property (nonatomic, assign)CGFloat picH;

@end

@implementation HWDetailPictureController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.saveBtn];
//    [self.view addSubview:self.shareBtn];
//    [self.view addSubview:self.commentBtn];
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.progressView];
    [self.scrollView addSubview:self.imageView];
    
    [self getBigImage];
    
}
#pragma mark --- 加载图片 ---
- (void)getBigImage
{
    if ([_model.type isEqualToString:@"gif"])
    {
        [self.imageView sd_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:self.model.gif.images.lastObject] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            CGFloat progress = 1.0 * receivedSize / expectedSize;
            self.progressView.progressLabel.text = [NSString stringWithFormat:@"%.1f%%", progress * 100];
            [self.progressView setProgress:progress animated:YES];
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.progressView.hidden = YES;
        }];
    }
    else
    {
        [self.imageView sd_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:self.model.image.big.firstObject] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            CGFloat progress = 1.0 * receivedSize / expectedSize;
            self.progressView.progressLabel.text = [NSString stringWithFormat:@"%.1f%%", progress * 100];
            [self.progressView setProgress:progress animated:YES];
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.progressView.hidden = YES;
        }];
    }
    
}

#pragma mark --- 保存图片 ---
- (void)saveImageBtn
{
    if (self.imageView.image != nil)
    {
        UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:@"加载失败..."];
    }
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"保存失败"];
    }else{
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
}

#pragma mark --- 返回按钮 ---
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark --- 评论按钮 ---
- (void)comment
{
    NSLog(@"评论了");
}

#pragma mark --- 分享按钮 ---
- (void)share
{
    NSLog(@"分享了");
}

#pragma mark --- 懒加载控件 ---
- (UIButton *)backBtn
{
    if (_backBtn == nil)
    {
        _backBtn = [[UIButton alloc]init];
        [_backBtn setBackgroundImage:[UIImage imageNamed:@"show_image_back_icon"] forState:UIControlStateNormal];
        _backBtn.x = 20;
        _backBtn.y = 30;
        [[_backBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            [self back];
        }];
        [_backBtn sizeToFit];  // 使控件的size和内容的size相同
    }
    return _backBtn;
}

- (UIButton *)shareBtn
{
    if (_shareBtn == nil)
    {
        _shareBtn = [[UIButton alloc]init];
        [_shareBtn setBackgroundImage:[UIImage imageNamed:@"mainCellShare"] forState:UIControlStateNormal];
        NSString *sharCount = [NSString stringWithFormat:@"%ld", (long)self.model.forward];
        [_shareBtn setTitle:sharCount forState:UIControlStateNormal];
        _shareBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _shareBtn.x = SCREEN_WIDTH - 120;
        _shareBtn.y = SCREEN_HEIGHT - 35;
        [_shareBtn sizeToFit];
        
        [[_shareBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            [self share];
        }];
    }
    return _shareBtn;
}

- (UIButton *)commentBtn
{
    if (_commentBtn == nil)
    {
        _commentBtn = [[UIButton alloc]init];
        [_commentBtn setImage:[UIImage imageNamed:@"mainCellComment"] forState:UIControlStateNormal];
        NSString *commentCount = [NSString stringWithFormat:@"%ld", (long)self.model.comment];
        [_commentBtn setTitle:commentCount forState:UIControlStateNormal];
        _commentBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _commentBtn.x = SCREEN_WIDTH - 60;
        _commentBtn.y = SCREEN_HEIGHT - 35;
        [_commentBtn sizeToFit];
        
        [[_commentBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            [self comment];
        }];
    }
    return _commentBtn;
}

- (UIButton *)saveBtn
{
    if (_saveBtn == nil)
    {
        _saveBtn = [[UIButton alloc]init];
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        _saveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _saveBtn.x = 25;
        _saveBtn.y = SCREEN_HEIGHT - 35;
        [_saveBtn sizeToFit];
        [[_saveBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            [self saveImageBtn];
        }];
    }
    return _saveBtn;
}

- (UIImageView *)imageView
{
    if (_imageView == nil)
    {
        _imageView = [[UIImageView alloc]init];
        _imageView.userInteractionEnabled = YES;
        // 添加轻拍返回手势
        [_imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)]];
        
        if ([self.model.type isEqualToString:@"gif"])
        {
            CGFloat picW = SCREEN_WIDTH;
            self.picH = picW * self.model.gif.height / self.model.gif.width;
            _imageView.size = CGSizeMake(picW, self.picH);
            _imageView.centerY = SCREEN_HEIGHT * 0.5;
        }
        else
        {
            CGFloat picW = SCREEN_WIDTH;
            self.picH = picW * self.model.image.height / self.model.image.width;
            _imageView.size = CGSizeMake(picW, self.picH);
            _imageView.frame = CGRectMake(0, 0, picW, self.picH);
        }
    }
    return _imageView;
}

- (DALabeledCircularProgressView *)progressView{
    if (_progressView == nil) {
        _progressView = [[DALabeledCircularProgressView alloc]init];
        _progressView.size = CGSizeMake(100, 100);
        _progressView.centerY = SCREEN_HEIGHT / 2;
        _progressView.centerX = SCREEN_WIDTH / 2;
        _progressView.progressLabel.textColor = [UIColor whiteColor];
        _progressView.roundedCorners = 3;
        
    }
    return _progressView;
}

- (UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.frame = SCREEN_FRAME;
        CGFloat height = SCREEN_WIDTH * self.model.image.height / self.model.image.width;
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, height);
        _scrollView.backgroundColor = [UIColor blackColor];
        
    }
    return _scrollView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
